<template>
	<view class="content">
		<view class="sex">
			<view class="item" @click="checkMan()">
				<view class="text">男</view>
				<image v-if="isMan" class="check" src="../../static/images/ic_success_check_small.png">
			</view>
			<view class="item" @click="checkWom()">
				<view class="text">女</view>
				<image v-if="isWom" class="check" src="../../static/images/ic_success_check_small.png">
			</view>
		</view>
		<view class="button">
			 <button class="btn" @click="saveSex()">确定</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				uid: '',
				isMan: false,
				isWom: false,
				isSex: '',
			}
		},
		onLoad(option) {
			this.uid = option.uid;
			this.sex = option.sex;
			if(this.sex == '男')
			{
				this.isMan = true;
				this.isSex = 1;
			}
			if(this.sex == '女')
			{
				this.isWom = true;
				this.isSex = 2;
			}
		},
		methods: {
			checkMan() {
				// 选择男
				this.isMan = true;
				this.isWom = false;
				this.isSex = 1;
			},
			checkWom() {
				// 选择女
				this.isMan = false;
				this.isWom = true;
				this.isSex = 2;
			},
			saveSex() {
				// 保存设置的性别
				uni.request({
					url: 'https://www.yedushu.cn/api/savesex', // 保存设置的性别
					method:'POST',
					data: {
						uid: this.uid,
						sex: this.isSex
					},
					success: (res) => {
						// console.log(JSON.stringify(res.data.code));
						if(res.data.code == 1) {
							// 修改成功
							var userinfo = uni.getStorageSync('userinfo');
							// console.log(JSON.stringify(userinfo));
							userinfo['sex'] = this.isSex; // 设置对象中的值
							uni.setStorageSync('userinfo', userinfo);  // 重新存储到本地
							// console.log(JSON.stringify(userinfo));
							if(userinfo.sex == 1)
							{
								var setsex = '男';
							}
							if(userinfo.sex == 2)
							{
								var setsex = '女';
							}
							// 触发资料与帐号设置（setmyselft.vue）页上的事件，更新性别
							uni.$emit("setsex",{
									setsex: setsex,
								},
							);
							// 返回到上一页面
							uni.navigateBack({
							    delta: 1
							});
						}
					}
				});
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		padding: 10px 10px;
	}
	.item {
		padding: 5px 0;
		border-bottom: solid 1px #F2F2F2;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.check {
		width: 40px;
		height: 40px;
	}
	.text {
		font-size: 15px;
		padding: 10px 0;
	}
	.button {
		padding-top: 20px;
	}
	.btn {
		background-color: #00cc33;
		color: #FFFFFF;
	}
</style>
